this code + loads the raw data - assigns factors to variables etc, load functions + conducts piecewise linear functions to the data (collapsed over blocks) + removes trials up to the breakpoint + saves data as a dataframe

knitr::opts_chunk$set(fig.width = 6, fig.height = 6, 
                      echo = TRUE, highlight = TRUE,
                      root.dir = "~/Dropbox/BHAMPROJECTS/RelValue_StudyProgramme/PROJECT_REPOSITORIES/ADDBIAS_REPOS/EXP2/ANALYSIS")
## Loading required package: wesanderson
## Loading required package: plyr
## Loading required package: ggplot2
## Loading required package: reshape2
## Warning in rows$sub = levels(dat$sub): Coercing LHS to a list

block switches as learning should be demonstrated by a speeding of RTs over the block, now going to plot the change in RTs (collapsed across conditions) after each block switch, and apply piecewise linear regression method to remove trials contaminated by the switch (i.e. remove trials occurring prior to the breakpoint) note: as some participants have more trials than others the number of trials included in the regression were titrated for each subject by starting with N = 200 and reducing by 5 until a solution was found

sub 501
switch.rows = array()
switch.rows$sub = levels(dat$sub)

switch.rows$num[switch.rows$sub == "501"] = clean.out.block.switches(dat[dat$sub == "501",], 200)

sub 502
switch.rows$num[switch.rows$sub == "502"] = clean.out.block.switches(dat[dat$sub == "502",], 200)

##### sub 503

switch.rows$num[switch.rows$sub == "503"] = clean.out.block.switches(dat[dat$sub == "503",], 200)

##### sub 504

switch.rows$num[switch.rows$sub == "504"] = clean.out.block.switches(dat[dat$sub == "504",], 200)

##### sub 505

switch.rows$num[switch.rows$sub == "505"] = clean.out.block.switches(dat[dat$sub == "505",], 180)

sub 506
switch.rows$num[switch.rows$sub == "506"] = clean.out.block.switches(dat[dat$sub == "506",],170)

sub 507
switch.rows$num[switch.rows$sub == "507"] = clean.out.block.switches(dat[dat$sub == "507",],180)

sub 508
switch.rows$num[switch.rows$sub == "508"] = clean.out.block.switches(dat[dat$sub == "508",], 180)

##### sub 509

switch.rows$num[switch.rows$sub == "509"] = clean.out.block.switches(dat[dat$sub == "509",],190)

sub 510
switch.rows$num[switch.rows$sub == "510"] = clean.out.block.switches(dat[dat$sub == "510",], 140)

sub 511
switch.rows$num[switch.rows$sub == "511"] = clean.out.block.switches(dat[dat$sub == "511",], 200)

sub 512
switch.rows$num[switch.rows$sub == "512"] = clean.out.block.switches(dat[dat$sub == "512",], 200)

sub 513
switch.rows$num[switch.rows$sub == "513"] = clean.out.block.switches(dat[dat$sub == "513",], 190)

sub 514
switch.rows$num[switch.rows$sub == "514"] = clean.out.block.switches(dat[dat$sub == "514",], 180)

sub 515
switch.rows$num[switch.rows$sub == "515"] = clean.out.block.switches(dat[dat$sub == "515",], 180)

sub 516
switch.rows$num[switch.rows$sub == "516"] = clean.out.block.switches(dat[dat$sub == "516",], 180)

sub 517
switch.rows$num[switch.rows$sub == "517"] = clean.out.block.switches(dat[dat$sub == "517",], 200)

sub 518
switch.rows$num[switch.rows$sub == "518"] = clean.out.block.switches(dat[dat$sub == "518",], 130)

sub 519
switch.rows$num[switch.rows$sub == "519"] = clean.out.block.switches(dat[dat$sub == "519",], 200)

sub 520
switch.rows$num[switch.rows$sub == "520"] = clean.out.block.switches(dat[dat$sub == "520",], 180)

sub 521
switch.rows$num[switch.rows$sub == "521"] = clean.out.block.switches(dat[dat$sub == "521",], 200)

sub 522
switch.rows$num[switch.rows$sub == "522"] = clean.out.block.switches(dat[dat$sub == "522",], 200)

sub 523
switch.rows$num[switch.rows$sub == "523"] = clean.out.block.switches(dat[dat$sub == "523",], 200)

sub 524
switch.rows$num[switch.rows$sub == "524"] = clean.out.block.switches(dat[dat$sub == "524",], 200)

sub 525
switch.rows$num[switch.rows$sub == "525"] = clean.out.block.switches(dat[dat$sub == "525",], 200)

sub 526
switch.rows$num[switch.rows$sub == "526"] = clean.out.block.switches(dat[dat$sub == "526",], 200)

remove trials occurring prior to the break point

rem.post.switch <- function(data, rows){
  
    n_row = rows$num[rows$sub == data$sub[1]]
    trials = length(data$sub)
    mark.trials = array(data=0, dim=trials)
    for (x in 2:(trials - 25)){
      if (data$fixprob[x-1] != data$fixprob[x]){
        trials_rem = which(data$resp[c(x:(x+50))] == data$cor_resp[c(x:(x+50))])
        trials_rem = trials_rem[n_row]
        mark.trials[c(x:(x+trials_rem-1))] = 1
      }}
    data$mark = mark.trials
    data = data[data$mark < 1, ]
    return(data)
}

tmp = by(dat, dat$sub, rem.post.switch, switch.rows)
dat = as.data.frame(do.call(rbind, tmp))
rm(tmp)

save data for analysis

# save.image("exp2_clean_BS_v1_28_02_17.R")